package com.dhcc.core.modules.system.controller;


import com.alibaba.fastjson.JSONObject;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.SplitTablesEntity;
import com.dhcc.core.modules.system.service.ISplitTablesService;
import com.dhcc.core.modules.system.warpper.RoleWarpper;
import com.dhcc.core.modules.system.warpper.SplitTableWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统分表对象表
 * 
 * @ClassName: SplitTablesController
 * @Description: TODO
 * @author syjz
 * @date 2022-03-14 14:04:56
 */
@Controller
@RequestMapping("${sys.backendPath}/splittable")
public class SplitTablesController extends BaseController {

	private String PREFIX = "/backend/system/splittable/";

	@Autowired
	private ISplitTablesService splitTablesService;
	

    /**
     * 跳转到系统分表对象表列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "splittable.html";
    }
	
    /**
     * 跳转到新增系统分表对象表
     */
    @RequestMapping("/to_add")
    public String toAdd(Model model) {
        Map map = new HashMap();
        map.put("tableName", "t_cdc_bs");
        map.put("sort", "tableName");
        JSONObject tables = this.splitTablesService.queryTableMap(map);

        model.addAttribute("tableMap",tables);
        return PREFIX + "splittable_add.html";
    }
	
	/**
     * 跳转到修改系统分表对象表
     */
    @RequestMapping("/to_edit/{splitTableId}")
    public String toUpdate(@PathVariable Long splitTableId, Model model) {
        SplitTablesEntity splitTables = this.splitTablesService.selectById(splitTableId);
        Object r = super.warpObject(new SplitTableWarpper(splitTables));

        Map map = new HashMap();
        map.put("tableName", "t_cdc_bs");
        map.put("sort", "tableName");
        JSONObject tables = this.splitTablesService.queryTableMap(map);

        model.addAttribute("splitTables",r);
        model.addAttribute("tableMap",tables);

        return PREFIX + "splittable_edit.html";
    }

    @RequestMapping(value = "/queryColumn/{tableName}")
    @ResponseBody
    public Object queryColumn(@PathVariable(required = false) String tableName) {
        return this.splitTablesService.queryColumn(tableName);
    }

    /**
     * 获取系统分表对象表列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        List<Map<String, Object>> roles = this.splitTablesService.selectTables(super.getParameter("tableCode"));
        return super.warpObject(new RoleWarpper(roles));
    }

    /**
     * 新增系统分表对象表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @BussinessLog(value = "新增系统分表对象表")
    public Object add(SplitTablesEntity splitTables) {
        if (CommonUtil.isOneEmpty(splitTables)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        splitTables.setId(null);
        splitTables.init();
        splitTables.insert();
        return R.ok();
    }

    /**
     * 删除系统分表对象表
     */
    @RequestMapping(value = "/remove")
    @ResponseBody
    @BussinessLog(value = "删除系统分表对象表")
    public Object delete(@RequestParam Long splitTablesId) {
	
        if (CommonUtil.isEmpty(splitTablesId)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }

        try {
            this.splitTablesService.delTableById(splitTablesId);
        } catch (BizException e) {
            return R.error(e.getMsg());
        }

        return R.ok();
    }

    /**
     * 修改系统分表对象表
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    @BussinessLog(value = "修改系统分表对象表")
    public Object update(SplitTablesEntity splitTables) {
        if (CommonUtil.isOneEmpty(splitTables)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        SplitTablesEntity old = this.splitTablesService.selectById(splitTables.getId());
        old.setTableComment(splitTables.getTableComment());
        old.setTableName(splitTables.getTableName());
        old.setParentId(splitTables.getParentId());
        old.setJoinField(splitTables.getJoinField());
        old.init();
        old.updateAllColumnById();
        return R.ok();
    }

    /**
     * 获取数据表列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        return this.splitTablesService.tree();
    }
	
}
